local screenW, screenH = guiGetScreenSize()
local w,h = guiGetScreenSize()
local sX, sY = guiGetScreenSize()
local startY = (sY - 220)/2
opcjaWybrana = 1

function stworzListe()
opcje = {}

local veh = getPedOccupiedVehicle(localPlayer)
	if veh then
			if not getVehicleEngineState(veh) then
			table.insert(opcje, {"Odpal silnik", 1})
			else
			table.insert(opcje, {"Zgaś silnik", 2})
			end
		  

			if getVehicleOverrideLights(veh) ~= 2 then
			table.insert(opcje, {"Uruchom lampy", 3})
			else
			table.insert(opcje, {"Zgaś lampy", 4})
			end

			if isVehicleLocked(veh) then
			table.insert(opcje, {"Otwórz zamek", 5})
			else
			table.insert(opcje, {"Zamknij zamek", 6})
			end
			
						if isElementFrozen(veh) then
			table.insert(opcje, {"Spuść ręczny", 7})
			else
			table.insert(opcje, {"Zaciągnij ręczny", 8})
			end
            
			
			if getVehicleDoorOpenRatio(veh,0) == 0 then
            table.insert(opcje, {"Otwórz maskę", 9})
			else
			table.insert(opcje, {"Zamknij maskę", 10})
			end
			

            if getVehicleDoorOpenRatio(veh,1) == 0 then
            table.insert(opcje, {"Otwórz bagażnik", 11})
			else
			table.insert(opcje, {"Zamknij bagażnik", 12})
			end
		  local id = getElementModel (veh ) -- get the model ID of the vehicle
           if id ~= 462 then
			if getElementData(veh,"pasyzapiete") then
            table.insert(opcje, {"Odepnij pasy", 13})
			else
			table.insert(opcje, {"Zapnij pasy", 14})
			end
			end
			if getElementData(veh,"Zamontowane:LPG") == "Tak" then
		    if getElementData(veh,"vehicle:LPGWloczone") then
            table.insert(opcje, {"Przełącz na Pb95", 15})
			else
			table.insert(opcje, {"Przełącz na LPG", 16})
			end
			end
		end		
if #opcje > 0 then
		if not show then
		bindKey("mouse_wheel_up", "down", onSuwakZmien)
		bindKey("mouse_wheel_down", "down", onSuwakZmien)
		bindKey("arrow_u", "down", onSuwakZmien)
		bindKey("arrow_d", "down", onSuwakZmien)
		bindKey("space", "down", onSuwakWybierz)
		SxS = getTickCount()
		addEventHandler("onClientRender", getRootElement(), dxDrawPanelInterakcji)
		show = true
	end
end
end

function ostatniPojazd()
local veh = getElementData(localPlayer, "lastVeh")
	if veh then
		if isElement(veh) then
		local driver = getElementData(veh, "lastDriver")
			if driver == localPlayer then
			local dx, dy, dz = getElementPosition(localPlayer)
			local vx, vy, vz = getElementPosition(veh)
				if 10 >= getDistanceBetweenPoints3D(dx, dy, dz, vx, vy, vz) then
				saveV = veh
				return veh
				end
			end
		end
	end
end
function wlaczPanelInterakcji()
local veh = getPedOccupiedVehicle(localPlayer)
	if veh then
    local id = getElementModel (veh ) -- get the model ID of the vehicle
    if id == 481 then return end -- if theVehicle is either Shamal or AT-400
--if getElementData(getPedOccupiedVehicle(localPlayer),"Zamontowane:LPG") == "Tak" then return end
		if getVehicleController(veh) == localPlayer then	   
		showPlayerHudComponent("radar",false)
		showChat(false)
		opcjaWybrana = 1
		stworzListe()
		end
	else
	local veh = ostatniPojazd()
		if veh then
		opcjaWybrana = 1
		stworzListe()
		end
	end
end

function wylaczPanelInterakcji()
	if show then
	show = false		
	showPlayerHudComponent("radar",true)
	showChat(true)
	unbindKey("mouse_wheel_up", "down", onSuwakZmien)
	unbindKey("mouse_wheel_down", "down", onSuwakZmien)
	unbindKey("arrow_u", "down", onSuwakZmien)
	unbindKey("arrow_d", "down", onSuwakZmien)
	unbindKey("space", "down", onSuwakWybierz)
	removeEventHandler("onClientRender", getRootElement(), dxDrawPanelInterakcji)
	saveV = nil
	end
end

bindKey("lshift", "down", wlaczPanelInterakcji)
bindKey("lshift", "up", wylaczPanelInterakcji)

function dxDrawPanelInterakcji()    
	for i,v in pairs(opcje) do
	pos = startY + ((i-1)*60)
		if i == opcjaWybrana then
                local Rx, Rf = interpolateBetween(0, 0, 0, screenW * 0.1881, screenW * 0.1784, 0, (getTickCount()-SxS)/500, "OutBack") 
                dxDrawRectangle(screenW * 0.0000, pos - 4, Rx, screenH * 0.0586, tocolor(0, 0, 0, 172), false)

local veh = getPedOccupiedVehicle(localPlayer)
		if isElementFrozen(veh) then
		     dxDrawImage(screenW * 0.0066, screenH * 0.5859, screenW * 0.0329, screenH * 0.0586, "Img/x4.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
                     dxDrawImage(screenW * 0.0066, screenH * 0.5859, screenW * 0.0329, screenH * 0.0586, "Img/x6.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end

		local silnik = getVehicleEngineState(veh)
		if silnik == false then
		        dxDrawImage(screenW * 0.0066, screenH * 0.3516, screenW * 0.0329, screenH * 0.0586, "Img/x6.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
		        dxDrawImage(screenW * 0.0066, screenH * 0.3516, screenW * 0.0329, screenH * 0.0586, "Img/x3.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end
		
		if isVehicleLocked(veh) then
			dxDrawImage(screenW * 0.0066, screenH * 0.5067, screenW * 0.0329, screenH * 0.0586, "Img/x7.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
			dxDrawImage(screenW * 0.0066, screenH * 0.5067, screenW * 0.0329, screenH * 0.0586, "Img/x6.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end
		
		if getVehicleDoorOpenRatio(veh,0) == 0 then
			dxDrawImage(screenW * 0.0066, screenH * 0.6630, screenW * 0.0329, screenH * 0.0586, "Img/x6.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
			dxDrawImage(screenW * 0.0066, screenH * 0.6630, screenW * 0.0329, screenH * 0.0586, "Img/x1.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end	
		
		if getVehicleDoorOpenRatio(veh,1) == 0 then
			dxDrawImage(screenW * 0.0066, screenH * 0.7397, screenW * 0.0329, screenH * 0.0586, "Img/x6.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
			dxDrawImage(screenW * 0.0066, screenH * 0.7397, screenW * 0.0329, screenH * 0.0586, "Img/x5.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end
       
		if getVehicleOverrideLights(veh) ~= 2 then
			dxDrawImage(screenW * 0.0066, screenH * 0.4297, screenW * 0.0329, screenH * 0.0586, "Img/x6.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
			dxDrawImage(screenW * 0.0066, screenH * 0.4297, screenW * 0.0329, screenH * 0.0586, "Img/x2.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end
	   
	   local id = getElementModel (veh )
        if id ~= 462 then
		if getElementData(veh,"pasyzapiete") then
			dxDrawImage(screenW * 0.0066, screenH * 0.8164, screenW * 0.0329, screenH * 0.0586, "Img/x10.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
			dxDrawImage(screenW * 0.0066, screenH * 0.8164, screenW * 0.0329, screenH * 0.0586, "Img/x6.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end
		end
		
		if getElementData(veh,"Zamontowane:LPG") == "Tak" then
		if getElementData(veh,"vehicle:LPGWloczone") then
			dxDrawImage(screenW * 0.0066, screenH * 0.8931, screenW * 0.0329, screenH * 0.0586, "Img/x9.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		else
			dxDrawImage(screenW * 0.0066, screenH * 0.8931, screenW * 0.0329, screenH * 0.0586, "Img/x8.png", 0, 0, 0, tocolor(255, 255, 255, 255), false)
		end
		end

		dxDrawText(v[1], screenW * 0.0073, pos, screenW * 0.1950, pos + 30, tocolor(255,255,255), 1.38, "clear", "center", "center",false, false, true, false, false)
		else
		dxDrawText(v[1], screenW * 0.0073, pos, screenW * 0.1950, pos + 30, tocolor(255,255,255), 1.25, "clear", "center", "center",false, false, true, false, false)
		end
	end
	
	if #opcje == 1 then
	opcjaWybrana = 1
	end

stworzListe()
end

function onSuwakZmien(k,ks)
	if k  == "mouse_wheel_up" then --na dol
	opcjaWybrana = opcjaWybrana - 1
        playSound("Tik.mp3")
        SxS = getTickCount()
		if opcjaWybrana == 0 then
		opcjaWybrana = #opcje
		end
	else
        playSound("Tik.mp3")
        SxS = getTickCount()
	opcjaWybrana = opcjaWybrana + 1
		if opcjaWybrana == #opcje + 1 then
		opcjaWybrana = 1
		end
	end
end

function onSuwakZmien(k,ks)
	if k  == "arrow_u" then --na dol
        playSound("Tik.mp3")
        SxS = getTickCount()
	opcjaWybrana = opcjaWybrana - 1
		if opcjaWybrana == 0 then
		opcjaWybrana = #opcje
		end
	else
        playSound("Tik.mp3")
        SxS = getTickCount()
	opcjaWybrana = opcjaWybrana + 1
		if opcjaWybrana == #opcje + 1 then
		opcjaWybrana = 1
		end
	end
end

function greetingHandler ()
	sound = playSound("start.mp3")
	setSoundVolume(sound, 0.5)
	setTimer( function()
    if isElement(sound) then 
    stopSound( sound )
    end
	end, 1.6*1000, 1 ) 
end
addEvent( "startsilnika", true )
addEventHandler( "startsilnika", localPlayer, greetingHandler )


function onSuwakWybierz()
	if #opcje == 1 then
	triggerServerEvent("onInterakcjeWybierzAkcje", localPlayer, false, saveV)
	return
	end

	if opcjaWybrana then
	local id = opcje[opcjaWybrana][2]
		if id then
		triggerServerEvent("onInterakcjeWybierzAkcje", localPlayer, id, saveV)
		end
	end
end
